package com.vitabuybuy.tv.ui.mine.talent.live

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityLiveStatisticsBinding
import com.library.common.base.BaseActivity
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.util.DateUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.dto.ReCallBackBean
import com.vitabuybuy.tv.ui.home.adapter.GoodsAdapter
import com.vitabuybuy.tv.ui.mine.talent.live.adapter.LiveDetailsGoodsAdapter
import com.vitabuybuy.tv.ui.mine.talent.live.viewmodel.LiveManageViewModel
import com.vitabuybuy.tv.ui.shopcenter.store.LiveReCallBackActivity
import com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.widget.LiveGoofsPopup
import kotlinx.android.synthetic.main.activity_live_statistics.*

class LiveStatisticsActivity :
    BaseActivity<LiveManageViewModel, ActivityLiveStatisticsBinding>() {

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_live_statistics

    private var liveId: Int? = null

    private val mAdapter by lazy { LiveDetailsGoodsAdapter() }

    override fun onGetBundle(bundle: Bundle?) {
        liveId = bundle?.getInt("liveId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.live_statistics))
        lvGoods.apply {
            adapter = mAdapter
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getLiveDetail(liveId)
        viewModel.liveDetils.observe(this, Observer {
            tvDuration.text = DateUtil.formatSeconds2(it.pushDuration / 1000)
            tvMostOnline.text = it.onlineQuantity.toString()
            tvTotalAudienceAmount.text = it.viewerQuantity.toString()
            tvAddFansAmount.text = "缺少字段"
            tvLikeAmount.text = it.praiseQuantity.toString()
            tvGiftAmount.text = it.giftQuantity.toString()
            mAdapter.setNewData(it.productList)
            ImageAdapter.setNomalImageUrl(
                ivCover,
                it.image,
                ContextCompat.getDrawable(mContext, R.drawable.default_image)
            )
            tvLiveTitle.text = it.title
            tvLiveStartTime.text = String.format(getString(R.string.start_live), it.openTime)
            tvLiveDuration.text = String.format(
                getString(R.string.duration),
                DateUtil.formatSeconds2(it.pushDuration)
            )
        })
    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivPlay, ivCover, tvSeeMore, tvSalesOrderList) { v ->
            when (v) {
                ivPlay, ivCover -> {
                    viewModel.liveDetils.value?.apply {
                        val bean = ReCallBackBean(
                            playUrl =
                            /**if(item.replayAddress != null)*/
                            /**if(item.replayAddress != null)*/
                            this.playUrl,
                            /**else "https://v-cdn.zjol.com.cn/276984.mp4"*/
                            /**else "https://v-cdn.zjol.com.cn/276984.mp4"*/
                            mCoverUrl = this.image,
                            mTitle = this.title,
                            audienceCount = this.viewerQuantity,
                            authorName = this.executor.realname,
                            authorId = this.executor.id.toString(),
                            authorRegion = "",
                            authorAvatar = this.executor.avatar,
                            attention = false,
                        )
                        LiveReCallBackActivity.openActivity(mContext, bean)
                    }
                }
                tvSeeMore -> {
                    LiveReplayListActivity.openActivity(mContext)
                }
                tvSalesOrderList -> {
                    LiveSalesStatisticsActivity.openActivity(mContext)
                }
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, liveId: Int) {
            mContext.startActivity(
                bundleOf(Pair("liveId", liveId)),
                LiveStatisticsActivity::class.java
            )
        }
    }

}
